package com.hrsystem.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hrsystem.entity.DistributedList;
import com.hrsystem.entity.DistributedListDetail;
import com.hrsystem.entity.SalaryList;
import com.hrsystem.entity.SalaryListDetail;
import com.hrsystem.lang.Result;
import com.hrsystem.mapper.SalaryListDetailMapper;
import com.hrsystem.mapper.SalaryListMapper;
import com.hrsystem.service.DistributedListDetailService;
import com.hrsystem.service.DistributedListService;
import com.hrsystem.service.SalaryListDetailService;
import com.hrsystem.service.SalaryListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;


@RestController
public class SalaryListController {

    @Autowired
    SalaryListService salaryListService;

    @Autowired
    DistributedListService distributedListService;

    @Autowired
    SalaryListDetailService salaryListDetailService;

    @Autowired
    DistributedListDetailService distributedListDetailService;

    @Resource
    SalaryListMapper salaryListMapper;

    @Resource
    SalaryListDetailMapper salaryListDetailMapper;

    //薪酬发放登记页面（简介）
    @Transactional
    @GetMapping("/SalaryDistributionRegistrationBriefly")
    public Result SalaryDistributionRegistrationBriefly(){
        QueryWrapper<SalaryList> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("IsRegister",false);
        queryWrapper.select("sList_id","fOrg_id","sOrg_id","tOrg_id","number");
        List<SalaryList> salaryDistributionRegistrationBriefly = salaryListMapper.selectList(queryWrapper);
        return Result.success(salaryDistributionRegistrationBriefly);
    }

    //薪酬发放登记页面的详情（通过id查询到所有）
    @Transactional
    @GetMapping("/SalaryDistributionRegistration/{id}")
    public Result SalaryDistributionRegistration(@PathVariable(name = "id") Long id){
        SalaryList salaryList = salaryListService.getById(id);
        return Result.success(salaryList);
    }

    //
    @Transactional
    @PostMapping("/RegisterSalary/{id}")
    public Result RegisterSalary(@PathVariable(name = "id") Long id,
                                 @RequestParam("registrant") String registrant) {
        SalaryList salaryList = salaryListService.getById(id);
        SalaryList temp;
        temp = new SalaryList();
        temp.setRegisterTime(LocalDateTime.now());
        temp.setIsRegister(true);
        temp.setIsReview(false);
        BeanUtil.copyProperties(salaryList, temp ,"RegisterTime","IsRegister","IsReview");
        temp.setRegistrant(registrant);
        salaryListService.saveOrUpdate(temp);
        return Result.success(null);
    }

    //薪酬发放复核简介
    @Transactional
    @GetMapping("/SalaryDistributionReviewBriefly")
    public Result SalaryDistributionReviewBriefly(){
        QueryWrapper<SalaryList> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("IsRegister",true);
        queryWrapper.eq("IsReview",false);
        queryWrapper.select("sList_id","fOrg_id","sOrg_id","tOrg_id","Registrant");
        List<SalaryList> salaryDistributionReviewBriefly = salaryListMapper.selectList(queryWrapper);
        return Result.success(salaryDistributionReviewBriefly);
    }

    //薪酬发放复核详情 根据id
    @Transactional
    @GetMapping("/SalaryDistributionReview/{id}")
    public Result SalaryDistributionReview(@PathVariable(name = "id") Long id){
        SalaryList salaryList = salaryListService.getById(id);
        return Result.success(salaryList);
    }

    //
    @Transactional
    @PostMapping("/ReviewSalary/{id}")
    public Result ReviewSalary(@PathVariable(name = "id") Long id) {
        SalaryList salaryList = salaryListService.getById(id);
        SalaryList temp;
        temp = new SalaryList();
        //TODO 复核人等登录写好再说
        temp.setIsReview(true);
        BeanUtil.copyProperties(salaryList, temp ,"IsReview");
        temp.setIsRegister(false);
        salaryListService.saveOrUpdate(temp);

        DistributedList distributedList=new DistributedList();
        distributedList.setSListId(temp.getSListId());
        distributedList.setForgId(temp.getForgId());
        distributedList.setSorgId(temp.getSorgId());
        distributedList.setTorgId(temp.getTorgId());
        distributedList.setNumber(temp.getNumber());
        distributedList.setRegisterTime(temp.getRegisterTime());
        distributedList.setRegistrant(temp.getRegistrant());

        QueryWrapper<SalaryListDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("sList_id",id);
        List<SalaryListDetail> salaryDistributionDetail = salaryListDetailMapper.selectList(queryWrapper);
        BigDecimal totalSalary = new BigDecimal(0);
        for (SalaryListDetail detail : salaryDistributionDetail) {
            totalSalary = totalSalary.add(detail.getBasic());
            totalSalary = totalSalary.add(detail.getTransportation());
            totalSalary = totalSalary.add(detail.getLunch());
            totalSalary = totalSalary.add(detail.getCommunication());
            totalSalary = totalSalary.add(detail.getEndowment());
            totalSalary = totalSalary.add(detail.getUnemployment());
            totalSalary = totalSalary.add(detail.getMedical());
            totalSalary = totalSalary.add(detail.getProvidentFund());
            if(detail.getBounty()!=null){
                totalSalary = totalSalary.add(detail.getBounty());
            }
            if(detail.getDeduction()!=null){
                totalSalary = totalSalary. subtract(detail.getDeduction());
            }
        }
        distributedList.setTotalsalary(totalSalary);

        distributedListService.saveOrUpdate(distributedList);

        for (SalaryListDetail detail : salaryDistributionDetail) {
            DistributedListDetail distributedListDetail =new DistributedListDetail();
            distributedListDetail.setDistributedId(distributedList.getDistributedId());
            distributedListDetail.setSListId(detail.getSListId());
            distributedListDetail.setFileId(detail.getFileId());
            distributedListDetail.setName(detail.getName());
            distributedListDetail.setBasic(detail.getBasic());
            distributedListDetail.setTransportation(detail.getTransportation());
            distributedListDetail.setLunch(detail.getLunch());
            distributedListDetail.setCommunication(detail.getCommunication());
            distributedListDetail.setEndowment(detail.getEndowment());
            distributedListDetail.setUnemployment(detail.getUnemployment());
            distributedListDetail.setMedical(detail.getMedical());
            distributedListDetail.setProvidentFund(detail.getProvidentFund());
            distributedListDetail.setBounty(detail.getBounty());
            distributedListDetail.setDeduction(detail.getDeduction());
            distributedListDetailService.save(distributedListDetail);

            detail.setBounty(new BigDecimal(0));
            detail.setDeduction(new BigDecimal(0));
            salaryListDetailService.saveOrUpdate(detail);
        }

        return Result.success(null);
    }


    @Transactional
    public void addSalaryNewList(SalaryList salaryList){
        salaryListService.saveOrUpdate(salaryList);
        }

}
